using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Threading;
using Microsoft.Win32;

namespace sb0t_winGUI
{
    class AutoStart
    {
        private static Mutex mutex = new Mutex();

        public static bool RunOnPowerUp = false;
        public static bool StartOnLoad = false;

        public static void SetAutoRunOnPowerUp(bool enable)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            
            if (enable)
            {
                try
                {
                    key.SetValue("sb0t", RunOnceFileUpdater.sbotpath + "sb0t.exe");
                }
                catch { }
            }
            else
            {
                try
                {
                    key.DeleteValue("sb0t", false);
                }
                catch { }
            }

            RunOnPowerUp = enable;
            key.Close();
            Save();
        }

        public static void SetAutoStartOnLoad(bool enable)
        {
            StartOnLoad = enable;
            Save();
        }

        public static void Load()
        {
            RunOnPowerUp = false;
            StartOnLoad = false;

            if (!File.Exists(RunOnceFileUpdater.sbotpath + "data/autostart.xml")) return;

            try
            {
                FileStream f = new FileStream(RunOnceFileUpdater.sbotpath + "data/autostart.xml", FileMode.Open);
                XmlReader xml = XmlReader.Create(new StreamReader(f));

                xml.MoveToContent();
                xml.ReadSubtree().ReadToFollowing("autostart");
                xml.ReadToFollowing("details");
                xml.ReadSubtree().ReadToFollowing("run");
                RunOnPowerUp = bool.Parse(xml.ReadElementContentAsString());
                xml.ReadToFollowing("start");
                StartOnLoad = bool.Parse(xml.ReadElementContentAsString());

                xml.Close();
                f.Flush();
                f.Close();
            }
            catch { }
        }

        private static void Save()
        {
            mutex.WaitOne();

            try
            {
                XmlWriterSettings appearance = new XmlWriterSettings();
                appearance.Indent = true;
                XmlWriter xml = XmlWriter.Create(RunOnceFileUpdater.sbotpath + "data/autostart.xml", appearance);

                xml.WriteStartDocument();
                xml.WriteStartElement("autostart");
                xml.WriteStartElement("details");
                xml.WriteElementString("run", RunOnPowerUp.ToString());
                xml.WriteElementString("start", StartOnLoad.ToString());
                xml.WriteEndElement();
                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                xml.Close();
            }
            catch { }

            mutex.ReleaseMutex();
        }
    }
}
